package servlet;

import java.io.IOException;
import java.sql.Date;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import decoder.ChineseDecoder;

import model.Goods;

import factory.DaoFactory;

/**
 * Servlet implementation class AddGoodsServlet
 */
@WebServlet("/AddGoodsServlet")
public class AddGoodsServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddGoodsServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String name = ChineseDecoder.getUtf8FromIso8859_1(request.getParameter("name"));
		response.sendRedirect(request.getContextPath() + "/jsp/AddGoods.jsp?name=" + name);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String name = ChineseDecoder.getUtf8FromIso8859_1(request.getParameter("name"));
		int goodsId = 0;
		String goodsName = ChineseDecoder.getUtf8FromIso8859_1(request.getParameter("goodsName"));
		float price = 0;
		int quantity = 0;
		
		try {
			goodsId = Integer.parseInt(request.getParameter("goodsId"));
		} catch(NumberFormatException e) {
			response.sendRedirect(request.getContextPath() + "/jsp/ErrorPage.jsp?error=AddGoodsIdError&name=" + name);
			return;
		}
		
		try {
			price = Float.parseFloat(request.getParameter("price"));
			if (price < 0) {
				response.sendRedirect(request.getContextPath() + "/jsp/ErrorPage.jsp?error=AddGoodsPriceError&name=" + name);
				return;
			}
		} catch(NumberFormatException e) {
			response.sendRedirect(request.getContextPath() + "/jsp/ErrorPage.jsp?error=AddGoodsPriceError&name=" + name);
			return;
		}
		
		try {
			quantity = Integer.parseInt(request.getParameter("quantity"));
			if (quantity < 0) {
				response.sendRedirect(request.getContextPath() + "/jsp/ErrorPage.jsp?error=AddGoodsQuantityError&name=" + name);
				return;
			}
		} catch(NumberFormatException e) {
			response.sendRedirect(request.getContextPath() + "/jsp/ErrorPage.jsp?error=AddGoodsQuantityError&name=" + name);
			return;
		}
		
		String day = ChineseDecoder.getUtf8FromIso8859_1(request.getParameter("date"));
		Date date = null;
		switch (day) {
		case "day1":
			date = new Date(Calendar.getInstance().getTimeInMillis());
			break;
		case "day2":
			date = new Date(Calendar.getInstance().getTimeInMillis() + 24*3600000L);
			break;
		case "day3":
			date = new Date(Calendar.getInstance().getTimeInMillis() + 2*24*3600000L);
			break;
		case "day4":
			date = new Date(Calendar.getInstance().getTimeInMillis() + 3*24*3600000L);
			break;
		case "day5":
			date = new Date(Calendar.getInstance().getTimeInMillis() + 4*24*3600000L);
			break;
		case "day6":
			date = new Date(Calendar.getInstance().getTimeInMillis() + 5*24*3600000L);
			break;
		case "day7":
			date = new Date(Calendar.getInstance().getTimeInMillis() + 6*24*3600000L);
			break;
		default:
			DateFormat fmt =new SimpleDateFormat("yyyy-MM-dd");
			java.util.Date d = null;
			try {
				d = fmt.parse(day);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			date = new Date(d.getTime());
		}
		
		Goods goods = new Goods(goodsId, goodsName, price, quantity, date);
		boolean success = DaoFactory.getGoodsDao().save(goods);
		
		if (success) {
			response.sendRedirect(request.getContextPath() + "/GoodsManagementServlet?name=" + name);
		} else {
			response.sendRedirect(request.getContextPath() + "/jsp/ErrorPage.jsp?error=AddGoodsDatabaseError&name=" + name);
		}
		
	}

}
